//
//  ZPStatusPictureView.swift
//  ZPWeibo
//
//  Created by zhangpeng on 16/4/21.
//  Copyright © 2016年 zhangpeng. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
// cell 重用标识
private let ZPStatusPictureViewCellId = "ZPStatusPictureViewCellId"

//cell 间距
private let itemMargin: CGFloat = 5

//cell 的宽高
private let cellWH: CGFloat = (SCREENW - 20 - itemMargin * 2) / 3

class ZPStatusPictureView: UICollectionView {

    
    var pictureInfo:[ZPPictureInfo]?{
    
        didSet{
        
            countLabel.text = "\(pictureInfo?.count ?? 0)"
            let size = getPictureViewSize(pictureInfo?.count ?? 0)
            // 更改自己的约束 没有涉及到被人来约束的 需要使用update 更简单
            
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
            
            //更新数据
            self.reloadData()
        }
    }
    
     // 处理我们的配图size
    func getPictureViewSize(count:Int) -> CGSize{
    
           // 列数 和行数
        let col = count >= 3 ? 3 : count
        let row = (count - 1) / 3 + 1
        
        // 配图的宽高
        let width = CGFloat(col) * cellWH + CGFloat(col - 1) * itemMargin
        let height = CGFloat(row) * cellWH + CGFloat(row - 1) * itemMargin
        
        return CGSize(width: width, height: height)
        
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
    
         // 由于该控件原创微博和转发微博都需要使用 所以需要设置成透明色
        self.backgroundColor =  UIColor.clearColor()
        
        dataSource = self
        // 注册cell
        registerClass(ZPStatusPictureViewCell.self, forCellWithReuseIdentifier: ZPStatusPictureViewCellId)
          // 设置layout
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
          // 设置itemSize
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
        
        // 设置垂直和水平间距
        layout.minimumInteritemSpacing = itemMargin
        layout.minimumLineSpacing = itemMargin
        
        //添加控件
        addSubview(countLabel)
        //添加约束
        countLabel.snp_makeConstraints { (make) -> Void in
             make.center.equalTo(self)
        }
        
    }
    
      // MARK: - 懒加载控件
    private lazy var countLabel:UILabel = UILabel(textColor: UIColor.redColor(), textFont: 34)
    
}

//实现数据源方法
extension ZPStatusPictureView: UICollectionViewDataSource{

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return pictureInfo?.count ?? 0
    }
    
    func  collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ZPStatusPictureViewCellId, forIndexPath: indexPath)as!ZPStatusPictureViewCell
        
        // 给自定义cell赋值
        cell.pic = pictureInfo![indexPath.row]
        
        return cell
    }
    
}

//自定义cell
class ZPStatusPictureViewCell:UICollectionViewCell{

    
    var pic:ZPPictureInfo?{
    
        didSet{
        bgImageView.sd_setImageWithURL(NSURL(string: pic?.thumbnail_pic ?? ""), placeholderImage: UIImage(named: "timeline_card_top_background"))
            
            /*
            - 是否为.gif 图片
            - url 后缀为.gif === 动态图片
            - url 后缀不为.git === 正常图片
            
            */
            // 动态图片
            if let p = pic?.thumbnail_pic where p.hasSuffix(".gif"){
            
                gifImageView.hidden = false
            }else{
            
                gifImageView.hidden = true
                
            }
        
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
     // MARK: - 设置视图
    private func setupUI(){
    
        self.backgroundColor = UIColor.whiteColor()
        
        //添加控件
        contentView.addSubview(bgImageView)
        contentView.addSubview(gifImageView)
        
        //设置约束
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }

        gifImageView.snp_makeConstraints { (make) -> Void in
            make.trailing.bottom.equalTo(contentView)
        }
    }
    
    //懒加载
    private lazy var bgImageView:UIImageView = {
    
        let img = UIImageView(image: UIImage(named: "timeline_card_top_background"))
        //设置图片显示真实大小，这样不失真
        img.contentMode = .ScaleAspectFill
        
        img.clipsToBounds = true
        
        return img
        
    }()
    
     // 动态图片logo
    private lazy var gifImageView:UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
    
}



